﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class RetrieveBook_form : Form
    {
        public RetrieveBook_form()
        {
            InitializeComponent();
        }

        private void RetrieveBook_form_Load(object sender, EventArgs e)
        {
            //添加检索类型
            comboBox1.Items.Add("书名");
            comboBox1.Items.Add("作者");
            comboBox1.Items.Add("出版社");
            comboBox1.SelectedIndex = 0;
            dataGridView1.DataSource = DBhelper.GetDataTable("select bookid 条形码,bookname 书名,typename 类型,bookauthor 作者,pressname 出版社,pressmarknumber 书架号,amount 数量,useamount 已借阅数量 " +
                "from BookInfo a,BookType b,Press c,Pressmark d " +
                "where a.typeid = b.typeid and a.pressid = c.pressid and a.pressmarkid = d.pressmarkid ");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string type = comboBox1.Text;
            string word = textBox1.Text.Trim();

            string sql = "select bookid 条形码,bookname 书名,typename 类型,bookauthor 作者,pressname 出版社,pressmarknumber 书架号,amount 数量,useamount 已借阅数量 " +
                "from BookInfo a,BookType b,Press c,Pressmark d " +
                "where a.typeid = b.typeid and a.pressid = c.pressid and a.pressmarkid = d.pressmarkid ";

            if (word.Length > 0)
            {
                if(type == "书名")
                {
                    sql += $"and bookname like '%{word}%'";
                }
                else if(type == "作者")
                {
                    sql += $"and bookauthor like '%{word}%'";
                }
                else
                {
                    sql += $"and pressname like '%{word}%'";
                }
            }

            try
            {
                dataGridView1.DataSource = DBhelper.GetDataTable(sql);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
